package com.lightbox.android.photoprocessing;


import java.lang.ref.WeakReference;

import com.jihox.abookpro.R;
import com.lightbox.android.photoprocessing.utils.BitmapUtils;
import com.lightbox.android.photoprocessing.utils.FileUtils;
import com.lightbox.android.photoprocessing.utils.MediaUtils;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class PhotoFilterActivity extends Activity {
	private static FilterTask sFilterTask;
	private ImageView mImageView = null;
	private RadioGroup mFilterRadioGroup = null;
	private Bitmap mBitmap = null;
	private String mCurrentPhotoPath = null;
	private TextView back_btn = null;
	private TextView save_btn = null;
	private ProgressDialog mProgressDialog = null;
	private int mCurrentFilter = 0;
//	private static SavePhotoTask sSavePhotoTask;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_photo_filter);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		initView();
		mCurrentPhotoPath=getIntent().getStringExtra("mOriginalPhotoPath");
		loadPhoto(mCurrentPhotoPath);
		mImageView.setImageBitmap(mBitmap);
		mFilterRadioGroup.check(0);
	}

	private void initView() {
		back_btn = (TextView) findViewById(R.id.flt_back_btn);
		back_btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();

			}
		});
		save_btn = (TextView) findViewById(R.id.flt_button_Save);
		save_btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showSavingProgressDialog();
				String nsavepath = FileUtils.savePhoto(mBitmap,mCurrentPhotoPath,true);
				Intent mIntent=new Intent();
				mIntent.putExtra("filterImagePath", nsavepath);
				setResult(RESULT_OK, mIntent);
				hideProgressDialog();
				finish();

			}
		});
		mImageView = (ImageView)findViewById(R.id.imageViewPhoto);
		mFilterRadioGroup = (RadioGroup) findViewById(R.id.filter_radiogroup);
//		mFilterRadioGroup.check(id)
		for(int i = 0;i<PhotoProcessing.FILTERS.length;i++){//动态添加滤镜按钮
			RadioButton tempRB = new RadioButton(this);
			tempRB.setCompoundDrawablesWithIntrinsicBounds(null, this.getResources().getDrawable(PhotoProcessing.FILTERS[i]), null, null);
			tempRB.setButtonDrawable(new ColorDrawable(Color.TRANSPARENT));
			tempRB.setId(i);//设置按钮position
			tempRB.setPadding(10, 13, 0, 10);
			tempRB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					// TODO Auto-generated method stub
					if(isChecked){
						int pos = (Integer) buttonView.getId();

//						Log.i("msg_pos", pos+"");
						sFilterTask = new FilterTask(PhotoFilterActivity.this);
						mCurrentFilter = pos;
						sFilterTask.execute(mCurrentFilter);
					}
				}
			});
			mFilterRadioGroup.addView(tempRB);
		}
		
	}
	private void loadPhoto(String path) {//加载图片
		DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
		
		if (mBitmap != null) {
			mBitmap.recycle();
		}
		
		mBitmap = BitmapUtils.getSampledBitmap(path, displayMetrics.widthPixels, displayMetrics.heightPixels,true);
		
		if (mBitmap != null && !mBitmap.isMutable()) {
			mBitmap = PhotoProcessing.makeBitmapMutable(mBitmap);
		}
		
		int angle = MediaUtils.getExifOrientation(path);
		mBitmap = PhotoProcessing.rotate(mBitmap, angle);
		

	}
	private static class FilterTask extends AsyncTask<Integer, Void, Bitmap> {//异步加载滤镜
		WeakReference<PhotoFilterActivity> mActivityRef;
		
		public FilterTask(PhotoFilterActivity activity) {
			mActivityRef = new WeakReference<PhotoFilterActivity>(activity);
		}
		
		
		private PhotoFilterActivity getActivity() {
			if (mActivityRef == null) {
				return null;
			}
			
			return mActivityRef.get();
		}
		
		@Override
		protected void onPreExecute() {
			PhotoFilterActivity activity = getActivity();
			if (activity != null) {
				activity.showFilterProgressDialog();
				activity.showTempPhotoInImageView();
			}
		}
		
		@Override
		protected Bitmap doInBackground(Integer... params) {		
			PhotoFilterActivity activity = getActivity();
			
			if (activity != null) {
				activity.loadPhoto(activity.mCurrentPhotoPath);
				int position = params[0];
				Bitmap bitmap = PhotoProcessing.filterPhoto(activity.mBitmap, position);				
				return bitmap;
			}
			
			return null;
		}
		
		@Override
		protected void onPostExecute(Bitmap result) {	
			PhotoFilterActivity activity = getActivity();
			if (activity != null) {
				activity.mBitmap = result;
				activity.mImageView.setImageBitmap(result);
				activity.hideProgressDialog();
			}
		}
	}
	private void showFilterProgressDialog() {
		String message = "正在加载...";
		mProgressDialog = ProgressDialog.show(this, "", message);
	}

	private void hideProgressDialog() {
		if (mProgressDialog != null && mProgressDialog.isShowing()) {
			mProgressDialog.dismiss();
		}
	}
	private void showTempPhotoInImageView() {
		if (mBitmap != null) {
			Bitmap bitmap = Bitmap.createScaledBitmap(mBitmap, mBitmap.getWidth()/4, mBitmap.getHeight()/4, true);
			mImageView.setImageBitmap(bitmap);
		}
	}
	
	private void showSavingProgressDialog() {
		String message = "正在处理...";
		mProgressDialog = ProgressDialog.show(this, "", message);
	}
	
}
